-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swap ERC-20 support, update to toolkit v5 #99
Conversation
Updated dependencies detected. Learn more about Socket for GitHub ↗︎
|
Alternatively, we can use a different approach were we swap all incoming tokens into the gas token using @andresaiello please, let me know if you think this solution is ok or if we should try to fix |
@fadeev I agree with the approach. This contract was designed before ERC20 support. I like the first approach |
Then I need help debugging |
I've tried the second approach (swapping all to gas tokens, deducting the gas fee, then swapping to destination token), and I'm getting the same result (tMATIC -> Goerli USDC, I'm getting 0 gETH and a proper amount of USDC). (address gasZRC20, uint256 gasFee) = IZRC20(targetTokenAddress)
.withdrawGasFee();
uint256 gasOutput = SwapHelperLib._doSwap(
systemContract.wZetaContractAddress(),
systemContract.uniswapv2FactoryAddress(),
systemContract.uniswapv2Router02Address(),
zrc20,
amount,
gasZRC20,
0
);
SwapHelperLib._doSwap(
systemContract.wZetaContractAddress(),
systemContract.uniswapv2FactoryAddress(),
systemContract.uniswapv2Router02Address(),
gasZRC20,
gasOutput - gasFee,
targetTokenAddress,
0
); https://zetachain-athens-3.blockscout.com/address/0x6bf6ae7d9763eb1e42d8c7e9D31A5C23Be192a70 |
mmmm targetTokenAddress is USDC? I guess to calculate the fee should be the native token of destination chain |
@andresaiello you mean |
|
are we sure gasFee is returning >0 ? |
It is returning a positive integer, yes. |
For example, for Goerli USDC the gas fee is 3500000. https://zetachain-athens-3.blockscout.com/address/0xDf5C8854C2A9822e15470547e139451af892124F ![]() |
I'm just using public variables to output some debug information as events don't show up in the explorer. |
When I manually increase
|
omnichain/swap/contracts/Swap.sol
Outdated
outputAmount - gasFee | ||
); | ||
IZRC20(gasZRC20).approve(gasZRC20, gasFee); | ||
// IZRC20(targetTokenAddress).withdraw(recipientAddress, outputAmount); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why you don't need the abi.encode? will check if is the same without... IZRC20(targetTokenAddress).withdraw(abi.encodePacked(recipientAddress), outputAmount);
@andresaiello I don't think that's the case. Here's an example of me swapping 10 tMATIC for Goerli USDC. The state of the contract is a result of a single transaction. https://zetachain-athens-3.blockscout.com/address/0x7c732C6A614ecaD95B2953C624d39825f58C6F5C ![]() I’m using public variables to debug the contract. ![]() At the end of the day, the contract has |
In a separate contract I've also tried setting |
Could it be that it's getting confused because USDC has uses different decimal amount that gas tokens? |
👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎ This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored. |
@SocketSecurity ignore @types/[email protected] |
@andresaiello please, review. |
Looks like the swap example is not compatible with swapping to/from ERC-20s.
Currently, we're swapping the whole incoming amount to target ZRC-20.
example-contracts/omnichain/swap/contracts/Swap.sol
Lines 50 to 58 in 51373fb
Then we check if the gas token is not equal to the target ZRC-20.
example-contracts/omnichain/swap/contracts/Swap.sol
Lines 60 to 64 in 51373fb
When we want to swap tMATIC for USDC (Goerli), the target ZRC-20 (USDC) will not match the gas token (gETH), so the swap will fail. This is issue 1.
If I understand correctly, for the swap to succeed the contract has to have
uint256 gasFee
amount ofaddress gasZRC20
to cover the gas costs when withdrawing the target ZRC-20. Right now we're only swapping to the target ZRC-20 and the contract does not have gas tokens. This is issue 2.@andresaiello please, let me know if you agree that both issues are valid.
To solve these issues I'm proposing we do the following:
IZRC20(targetTokenAddress).withdrawGasFee();
SwapHelperLib.swapTokensForExactTokens
SwapHelperLib._doSwap
.For this to work I've added a
swapTokensForExactTokens
helper:https://github.com/zeta-chain/toolkit/blob/bc629af99832689697b2c45a6560b1b5df382d66/contracts/SwapHelperLib.sol#L127-L164
The problem I'm facing right now is that when I try to interact with a contract:
https://zetachain-athens-3.blockscout.com/address/0x7F665c57796E19a6A938f2B6dDbc5Ac5576c5De7
The contract isn't swapping into the gas token gETH (the balance is 0), but swaps fine to the destination token USDC.
I need help figuring out why
swapTokensForExactTokens
doesn't work as intended.